<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-row>
        <el-form-item label="审核时间筛选">
          <el-date-picker
            v-model="dataForm.time"
            type="daterange"
            value-format="yyyy-MM-dd"
            format="yyyy-MM-dd"
            range-separator="~"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="timeOptions"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="出货店铺">
          <el-input v-model="dataForm.outShopName" placeholder="出货店铺" clearable></el-input>
        </el-form-item>
        <el-form-item label="入货店铺">
          <el-input v-model="dataForm.inShopName" placeholder="入货店铺" clearable></el-input>
        </el-form-item>
        <el-form-item label="记录类型">
          <el-select v-model="dataForm.type" clearable placeholder="全部">
            <el-option
              v-for="type in types"
              :key="type.value"
              :label="type.label"
              :value="type.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="dataForm.status" clearable placeholder="全部">
            <el-option label="待审核" :value="1"></el-option>
            <el-option label="成功" :value="2"></el-option>
            <el-option label="审核失败" :value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">查询</el-button>
        </el-form-item>
      </el-row>
      <el-row style="margin-bottom: 15px;">
        <el-button v-if="isAuth('merchant:shop:stock')" type="primary" @click="churukuHandle(1)">出库</el-button>
        <el-button v-if="isAuth('merchant:shop:stock')" type="primary" @click="churukuHandle(2)">入库</el-button>
        <el-button v-if="isAuth('merchant:shop:stock')" type="primary" @click="churukuHandle(3)">移库</el-button>
      </el-row>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
      :header-cell-style="{background:'#17b3a3',color:'#fff'}"
    >
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="操作时间"
        width="160px"
      >
        <template slot-scope="{row}">{{dateFormat(row.createTime,'YYYY-MM-DD HH:mm:ss')}}</template>
      </el-table-column>
      <el-table-column prop="actionUsername" header-align="center" align="center" label="操作人"></el-table-column>
      <el-table-column prop="outShopName" header-align="center" align="center" label="出货店铺"></el-table-column>
      <el-table-column prop="inShopName" header-align="center" align="center" label="入货店铺"></el-table-column>
      <el-table-column prop="typeStr" header-align="center" align="center" label="类型"></el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="状态">
        <!-- 1-申请中/待审核,2-成功 -->
        <template slot-scope="{row}">
          <el-tag v-if="row.status == 1" type="info">待审核</el-tag>
          <el-tag v-else-if="row.status == 2" type="success">成功</el-tag>
          <el-tag v-else-if="row.status == 3" type="danger">审核失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="verifyTime" header-align="center" align="center" label="审核时间">
        <template slot-scope="{row}">
          <span v-if="row.verifyTime != null">{{dateFormat(row.verifyTime,'YYYY-MM-DD HH:mm:ss')}}</span>
          <span v-else></span>
        </template>
      </el-table-column>
      <el-table-column prop="verifyUsername" header-align="center" align="center" label="审核人"></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == 1"
            type="text"
            size="small"
            @click="audit(scope.row.id, 2)"
          >审核通过</el-button>
          <el-button
            v-if="scope.row.status == 1"
            type="text"
            size="small"
            @click="audit(scope.row.id, 3)"
          >审核不通过</el-button>
          <el-button type="text" size="small" @click="goodsListHandle(scope.row.id)">商品详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 出库 -->
    <Chuku v-if="chukuVisible" :companys="companys" ref="chuku" @refreshDataList="getDataList"></Chuku>
    <Ruku v-if="rukuVisible" :companys="companys" ref="ruku" @refreshDataList="getDataList"></Ruku>
    <Yiku v-if="yikuVisible" :companys="companys" ref="yiku" @refreshDataList="getDataList"></Yiku>
    <GoodsList v-if="goodsListVisible" ref="goodsList"></GoodsList>
  </div>
</template>

<script>
import { getCompanys } from "@/utils";
import Chuku from "./stockApplyForm-chuku";
import Ruku from "./stockApplyForm-ruku";
import Yiku from "./stockApplyForm-yiku";
import GoodsList from "./form-detail-goods-list";
export default {
  data() {
    return {
      dataForm: {
        time: [],
        outShopName: "",
        inShopName: "",
        type: null,
        status: null,
      },
      timeOptions: {
        // 大于当前时间不可选
        disabledDate(time) {
          return (
            time.getTime() > new Date(new Date().toLocaleDateString()).getTime()
          );
        }
      },
      types: [],
      companys: [],
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      chukuVisible: false,
      rukuVisible: false,
      yikuVisible: false,
      goodsListVisible: false
    };
  },
  components: {
    Chuku,
    Ruku,
    Yiku,
    GoodsList
  },
  mounted() {
    this.getTypes();
  },
  activated() {
    getCompanys(res => {
      this.companys = res;
    });
    this.getDataList();
  },
  methods: {
    init() {
      this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      let time = "";
      if (this.dataForm.time && this.dataForm.time.length == 2) {
        time = this.dataForm.time[0] + "~" + this.dataForm.time[1];
      }
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/merchant/shop/stock/applyForm/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          outShopName: this.dataForm.outShopName,
          inShopName: this.dataForm.inShopName,
          time: time,
          type: this.dataForm.type,
          status: this.dataForm.status,
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 出入库弹窗
    churukuHandle(actionType) {
      if (actionType == 1) {
        this.chukuVisible = true;
        this.$nextTick(() => {
          this.$refs.chuku.init();
        });
      } else if (actionType == 2) {
        this.rukuVisible = true;
        this.$nextTick(() => {
          this.$refs.ruku.init();
        });
      } else if (actionType == 3) {
        this.yikuVisible = true;
        this.$nextTick(() => {
          this.$refs.yiku.init();
        });
      }
    },
    // 商品列表
    goodsListHandle(id) {
      this.goodsListVisible = true;
      this.$nextTick(() => {
        this.$refs.goodsList.init(id);
      });
    },
    audit(formId, status) {
      this.$http({
        url: this.$http.adornUrl(`/merchant/shop/stock/applyForm/audit`),
        method: "post",
        data: this.$http.adornData({
          id: formId,
          status: status
        })
      }).then(({ data }) => {
        this.disabled = false;
        if (data && data.code === 0) {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 500,
            onClose: () => {
              this.getDataList();
            }
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    // 获取记录类型
    getTypes() {
      this.types = [];
      this.$http({
        url: this.$http.adornUrl("/merchant/shop/stock/applyForm/type"),
        method: "get",
        params: this.$http.adornParams({})
      }).then(({ data }) => {
        if (data && data.code == 0) {
          this.types = data.data;
        }
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.id;
          });
      this.$confirm("真的要删除所选项么?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl("/merchant/shop/stock/applyForm/delete"),
          method: "post",
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    }
  }
};
</script>